using System;
using System.Collections;
using System.Collections.Generic;
using BCW;
using Rilisoft.MiniJson;
using UnityEngine;
using UnityEngine.SceneManagement;

public class FirstLoading : MonoBehaviour
{
	public UISprite sprite;

	public UISprite sprite2;

	public UISprite noInternetSprite;

	public GameObject loading;

	public static bool loadSkinScene;

	public bool waitUntilSkinJsonLoad;

	public bool startWithInternet;

	private AsyncOperation load;

	private void Awake()
	{
		Screen.sleepTimeout = -1;
		Time.timeScale = 1f;
		noInternetSprite.alpha = 0f;
		loading.SetActive(false);
		GpgWrapper.instance.Init();
		StartCoroutine(LoadLevelMenu());
		SaveSettings();
	}

	private void SaveSettings()
	{
		if (!BCWStorage.Get<bool>("StandartJoystick"))
		{
			int num = Mathf.FloorToInt(133.33334f);
			int num2 = Mathf.FloorToInt(177.77779f);
			int num3 = Mathf.FloorToInt((float)Screen.width / (float)Screen.height * 100f);
			if (num3 == num)
			{
				BCWDebug.LogError("save settings joystick");
				PlayerPrefs.SetString("KEY_SAVE_Joystick", new ButtonPositionInfo(new Vector3(170f, 170f, 0f), 0.15f).ToString());
			}
			if (num3 == num2)
			{
				BCWDebug.LogError("save settings button");
				PlayerPrefs.SetString("KEY_SAVE_CarButtonRight", new ButtonPositionInfo(new Vector3(326f, 102f, 0f), 0.7f).ToString());
				PlayerPrefs.SetString("KEY_SAVE_CarButtonLeft", new ButtonPositionInfo(new Vector3(110f, 102f, 0f), 0.7f).ToString());
			}
			BCWStorage.Set("StandartJoystick", true);
		}
	}

	private void ParsDataFromJson(string json)
	{
		if (string.IsNullOrEmpty(json))
		{
			return;
		}
		BCWDebug.Log(json);
		try
		{
			Dictionary<string, object> dictionary = Json.Deserialize(json) as Dictionary<string, object>;
			Dictionary<string, object> dictionary2 = (Dictionary<string, object>)dictionary["TestElements"];
			foreach (KeyValuePair<string, object> item in dictionary2)
			{
				if (SkinExporterABTestPanel.jsonKey == item.Key || SkinExporterABTestPanel.minecraftLabelKey == item.Key || SkinExporterABTestPanel.startWithInternetKey == item.Key)
				{
					Dictionary<string, object> dictionary3 = (Dictionary<string, object>)item.Value;
					ABTestElement aBTestElement = new ABTestElement(item.Key, Convert.ToInt32(dictionary3["Percent"]), Convert.ToString(dictionary3["EnableForVersion"]));
					if (SkinExporterABTestPanel.minecraftLabelKey == item.Key)
					{
						SkinExporterABTestPanel.showMinecraftLabel = aBTestElement.IsActive();
					}
					else if (SkinExporterABTestPanel.startWithInternetKey == item.Key)
					{
						startWithInternet = aBTestElement.IsActive();
					}
					else
					{
						loadSkinScene = aBTestElement.IsActive();
					}
				}
			}
		}
		catch
		{
			SkinExporterABTestPanel.showMinecraftLabel = false;
			loadSkinScene = true;
			startWithInternet = false;
		}
	}

	public void OnClickBtnTryAgain()
	{
		noInternetSprite.alpha = 0f;
		loading.SetActive(true);
	}

	private IEnumerator LoadJSON(string url)
	{
		WWW downloadData = new WWW(url);
		waitUntilSkinJsonLoad = true;
		loading.SetActive(true);
		while (!downloadData.isDone)
		{
			yield return null;
		}
		if (!string.IsNullOrEmpty(downloadData.error))
		{
			string json = BCWStorage.Get<string>(SkinExporterABTestPanel.jsonKey);
			if (json != string.Empty)
			{
				ParsDataFromJson(json);
			}
			else
			{
				startWithInternet = true;
				loadSkinScene = true;
				SkinExporterABTestPanel.showMinecraftLabel = true;
			}
			if (startWithInternet)
			{
				noInternetSprite.gameObject.SetActive(true);
				noInternetSprite.alpha = 1f;
				loading.SetActive(false);
				yield return new WaitForSeconds(2f);
				StartCoroutine(LoadJSON(URLs.ABTest));
				yield break;
			}
		}
		else
		{
			BCWStorage.Set(SkinExporterABTestPanel.jsonKey, downloadData.text, true);
			ParsDataFromJson(downloadData.text);
		}
		noInternetSprite.alpha = 0f;
		loading.SetActive(false);
		waitUntilSkinJsonLoad = false;
	}

	public IEnumerator LoadLevelMenu()
	{
		sprite.alpha = 0f;
		sprite2.alpha = 0f;
		yield return new WaitForSeconds(0.5f);
		BCWStorage.Get<bool>("goldDevolvated");
		BCWStorage.Get<int>(settings.keyKolCoins);
		BCWStorage.Get<int>(settings.keyMonetBCount);
		BCWStorage.Get<bool>("keyNewSaveNickName");
		BCWStorage.Get<string>(settings.keyCryptoName);
		BCWStorage.Get<string>(settings.keyCryptoName);
		sprite.fillAmount = 0f;
		sprite.alpha = 1f;
		sprite2.alpha = 1f;
		if (Load.LoadBool(settings.keyTutorialActive))
		{
			BCWStorage.Set(settings.keyCryptoTutorialActive, true);
		}
		if (BCWStorage.Get<bool>(settings.keyCryptoTutorialActive))
		{
			load = SceneManager.LoadSceneAsync("Lobby");
		}
		else
		{
			load = SceneManager.LoadSceneAsync("Tutorial");
		}
		while (!load.isDone)
		{
			float tmp = load.progress * 100f / 100f;
			if (tmp > sprite.fillAmount)
			{
				sprite.fillAmount = tmp;
			}
			sprite2.fillAmount = sprite.fillAmount;
			yield return null;
		}
	}
}
